/*
 * @Author: along
 * @Date:  2020-02-20 13:47:00
 */
<template>
    <div class="container socialIM">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-box-content">
                    <span class="header-box-content-title">创建时间:</span>
                    <el-date-picker
                        v-model="head.time"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        size="small"
                        @change="getTableData(1)"
                    />
                </div>
                <div class="header-box-content">
                    <p class="header-box-content-title">备注:</p>
                    <el-input
                        v-model="head.content"
                        placeholder="请输入要搜索的内容"
                        suffix-icon="el-icon-search"
                        size="small"
                        clearable
                        class="search-ipt"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
            </div>
            <div class="header-right">
                <div class="header-box-content">
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnCreateIm()"
                    >新建表情</el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="socialIM"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="emoji_title"
                label="备注"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="image_url"
                label="表情"
                width="130px"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <template v-if="scope.row.image_url !== ''">
                        <img
                            :src="scope.row.image_url"
                            @click="fnShowPreview(scope.row.image_url)"
                            style="width:45px;height:45px"
                        >
                    </template>
                    <template v-else>
                        --
                    </template>
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="创建时间"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="emoji_sort"
                label="排序"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.emoji_sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEditIm(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeleteIm(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- dialog -->
        <el-dialog
            width="500px"
            custom-class="schedule-dialog"
            :visible.sync="showForbidden"
            center
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForbidden"
                label-width="80px"
            >
                <el-form-item
                    prop='content'
                    label="表情备注"
                >
                    <el-input
                        v-model="ruleForm.content"
                        placeholder="请输入表情备注"
                        size="small"
                    />
                </el-form-item>
                <el-form-item
                    label="图片"
                    prop="image"
                >
                    <el-upload
                        :show-file-list="false"
                        :http-request="e=> {fnUploadImageOfGoods(e, true)}"
                        accept="image/*"
                        class="upload-good-image"
                        action="https://jsonplaceholder.typicode.com/posts/"
                    >
                        <img
                            v-if="ruleForm.image"
                            :src="ruleForm.image"
                            class="avatar">
                        <p
                            class="upload-hide"
                            v-else
                        >
                            <i class="el-icon-plus"/>
                        </p>
                    </el-upload>
                </el-form-item>
                <div class="el-emit">
                    <el-button
                        type="primary"
                        size="small"
                        style="margin-right: 60px;"
                        @click="submitForm('ruleForbidden')">确认</el-button>
                    <el-button
                        size="small"
                        @click="resetForm('ruleForbidden')">取消</el-button>
                </div>
            </el-form>
        </el-dialog>
        <!-- dialog -->


        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import imgPreview from '@/components/common/img-preview';
import {getDate} from '@/assets/js/utils';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            id: '',
            type: '',
            ruleForm: {
                content:'',
                image: ''
            },
            rules:{
                content: [
                    { required: true, message: '请填写备注,不可以为空', trigger: 'change' }
                ],
                image:[
                    { required: true, message: '请上传图片,不可以为空', trigger: 'change' }
                ]
            },
            showForbidden: false,
            head: {
                time: '',
                content: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            filterData: {
                sort_field: 'asc',
                sort_type: 'desc'
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {
        showForbidden (val) {
            if (!val) {
                this.ruleForm = {
                    content:'',
                    image: ''
                };
                this.$refs['ruleForbidden'].resetFields();
            }
        }
    },
    mounted () {
        this.authResult = this.$route.meta.authResult;
        this.fnBreadcrumb();
        this.getTableData();
    },
    methods: {
        /**
         * 设置表单header
         */
        fnBreadcrumb () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '社交'},{title: 'IM表情包管理'}]);
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/chat_emoji%5Cget_list', {
                page_id: page || this.page.now_page,
                time_range: this.head.time !== '' ? (this.head.time[0].getTime() / 1000 + '-' + this.head.time[1].getTime() / 1000) : '',
                keyword: this.head.content,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 排序
         */
        fnEditSort (row) {
            this.$prompt('请输入排序号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '排序号格式不正确,请重新输入'
            }).then(({ value }) => {
                this.$post('/student/chat_emoji%5Cmodify',{
                    id: row.id,
                    emoji_sort: value
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 查看图片
         */
        fnShowPreview(images) {
            let img = [
                images
            ];

            this.$refs.preview.show(img);
        },

        /**
         * 上传图片
         */
        fnUploadImageOfGoods(e) {
            this.fnUploadImage(e, (resp) => {
                this.ruleForm.image = resp.data.url;
            }, (resp) => {
                console.log(resp.msg);
            });
        },
        fnUploadImage(e, success, error) {
            let timeTmp = new Date().getTime();
            let pic = new FormData();

            pic.append('file', e.file);
            this.$upload({
                url: '/__local/common%5Cupload',
                data: pic,
                success: resp => {
                    if (resp.code == 1) {
                        success(resp);
                    } else {
                        this.$message({
                            type: 'warning',
                            message: '上传图片失败',
                            center: true
                        });
                        error(resp);
                    }
                }
            });
        },

        /**
         * 取消
         */
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.showForbidden = !this.showForbidden;
        },

        /**
         * 新建表情
         */
        fnCreateIm () {
            this.type = 'add';
            this.showForbidden = !this.showForbidden;
        },

        /**
         * 编辑表情
         */
        fnEditIm (row) {
            this.type = 'edit';
            this.id = row.id;
            this.ruleForm = {
                content: row.emoji_title,
                image: row.image_url
            };
            this.showForbidden = !this.showForbidden;
        },

        /**
         * 确认添加表情
         */
        submitForm (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.type == 'add') {
                        this.$post('/student/chat_emoji%5Csave', {
                            id: '',
                            emoji_title: this.ruleForm.content,
                            image_url: this.ruleForm.image
                        }, resp =>{
                            if(resp.code == 1) {
                                this.getTableData(1);
                                this.showForbidden = !this.showForbidden;
                                this.$message({
                                    type: 'success',
                                    message: '添加成功',
                                    center: true
                                });
                            } else {
                                this.$message({
                                    type: 'warning',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    } else if (this.type == 'edit') {
                        this.$post('/student/chat_emoji%5Csave', {
                            id: this.id,
                            emoji_title: this.ruleForm.content,
                            image_url: this.ruleForm.image
                        }, resp =>{
                            if(resp.code == 1) {
                                this.getTableData(1);
                                this.showForbidden = !this.showForbidden;
                                this.$message({
                                    type: 'success',
                                    message: '编辑成功',
                                    center: true
                                });
                            } else {
                                this.$message({
                                    type: 'warning',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    } else {
                        return;
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 删除
         */
        fnDeleteIm (row) {
            this.$confirm('是否要删除此条数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/chat_emoji%5Cdelete', {
                    id: row.id
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 'asc',
                descending: 'desc'
            };

            let sortFieldMap = {
                create_time: 'create_time'
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },


        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-box-content {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .el-emit {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-left: 0px !important;
        margin-top: 30px;
    }
</style>
<style lang="less">
    .socialIM .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .socialIM .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .socialIM .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .socialIM .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
    .socialIM .upload-good-image{
        border: 1px solid red;
        width: 100px;
        height: 100px;
        display: flex;
        border: 1px dashed #d9d9d9;
        border-radius: 4px;
        align-items: center;
        justify-content: center;
        font-size: 34px;
        color: #8c939d;
        img{
            width: 100px;
            height: 100px;
        }
        .upload-hide {
            width: 100px;
            height: 100px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
</style>